package com.chengyu.core.controller.shop;

import cn.hutool.core.bean.BeanUtil;
import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.TrendsEnums;
import com.chengyu.core.domain.form.TrendsForm;
import com.chengyu.core.domain.result.WalkTrendsResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.*;
import com.chengyu.core.model.*;
import com.chengyu.core.service.member.MemberCollectShopService;
import com.chengyu.core.service.shop.ShopConfigService;
import com.chengyu.core.service.shop.ShopService;
import com.chengyu.core.service.walk.WalkMemberService;
import com.chengyu.core.service.walk.WalkTrendsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Api(tags = "店铺")
@Controller
@RequestMapping("/common")
public class ShopController extends UserBaseController {

    @Autowired
    private ShopService shopService;
    @Autowired
    private MemberCollectShopService memberCollectShopService;
    @Autowired
    private ShopConfigService shopConfigService;
    @Autowired
    private WalkTrendsService walkTrendsService;
    @Autowired
    private WalkMemberService walkMemberService;
    @Autowired
    private UmsShopOwnLevelMapper umsShopOwnLevelMapper;
    @Autowired
    private UmsShopLevelPriceMapper umsShopLevelPriceMapper;
    @Autowired
    private UmsShopLevelMapper umsShopLevelMapper;
    @Autowired
    private UmsShopLevelActionLogMapper umsShopLevelActionLogMapper;

    @Autowired
    private UmsShopLevelPriceOwnMapper umsShopLevelPriceOwnMapper;
    @Autowired
    private UmsShopLevelOwnMapper umsShopLevelOwnMapper;

    @ApiOperation(value = "店铺详情")
    @ResponseBody
    @RequestMapping(value = "/shop/get", method = RequestMethod.GET)
    public CommonResult<Map<String, Object>> getList(Integer shopId) {
        UmsShop shop = shopService.getShopById(shopId);
        Map<String, Object> map = BeanUtil.beanToMap(shop);
        //是否关注此店铺
        map.put("isCollectShop", memberCollectShopService.isCollectShop(getCurrentMemberOrNull(), shopId));
        map.put("cateStyle", shopConfigService.getShopConfig(shopId).getCateStyle());
        return CommonResult.success(map);
    }

    @ApiOperation(value = "店铺动态")
    @ResponseBody
    @RequestMapping(value = "/shop/getTrendsList", method = RequestMethod.GET)
    public CommonResult<CommonPage<WalkTrendsResult>> search(Integer shopId, Integer page, Integer pageSize) {
        TrendsForm form = new TrendsForm();
        form.setStatus(CommonConstant.SUS_INT);
        form.setWalkMemberId(walkMemberService.getWalkMemberByMember(shopService.getShopById(shopId).getMemberId()).getId());
        form.setType(TrendsEnums.TrendsType.GOODS.getValue());
        CommonPage<WalkTrendsResult> list = walkTrendsService.getTrendsList(null, 1, false, null, null, form, page, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "根据邀请码找店铺")
    @ResponseBody
    @RequestMapping(value = "/shop/getByInviteCode", method = RequestMethod.GET)
    public CommonResult<UmsShop> getShopByInviteCode(String inviteCode) throws ServiceException {
        UmsShop shop = shopService.getShopByInviteCode(inviteCode);
        return CommonResult.success(shop);
    }

    @ApiOperation(value = "修改店铺所属等级")
    @ResponseBody
    @RequestMapping(value = "/shop/updateShopLevel", method = RequestMethod.GET)
    public CommonResult<String> updateShopLevel(Integer shopId, Integer shopLevelPriceId, Integer type) throws ServiceException {
        UmsShopLevelActionLog umsShopLevelActionLog = new UmsShopLevelActionLog();
        umsShopLevelActionLog.setShopId(shopId);
        umsShopLevelActionLog.setType(type);
        if (type == 1) {
            //如果是会员等级升级，才添加这里
            UmsShop shop = new UmsShop();
            shop.setId(shopId);
            shop.setLevel(shopLevelPriceId);
            shop.setLevelStartTime(new Date());
            shopService.updateShop(shop);
        }
        //同时添加店铺拥有的等级
        UmsShopLevelPrice umsShopLevelPrice = umsShopLevelPriceMapper.selectByPrimaryKey(shopLevelPriceId);
        UmsShopLevel umsShopLevel = umsShopLevelMapper.selectByPrimaryKey(umsShopLevelPrice.getLevelId());
        //添加到UmsShopLevelPriceOwn,UmsShopLevelOwn
        UmsShopLevelOwn umsShopLevelOwn = new UmsShopLevelOwn();
        umsShopLevelOwn.setShopId(shopId);
        umsShopLevelOwn.setCode(umsShopLevel.getCode());
        umsShopLevelOwn.setType(umsShopLevel.getType());
        umsShopLevelOwn.setName(umsShopLevel.getName());
        umsShopLevelOwn.setLevel(umsShopLevel.getLevel());
        umsShopLevelOwn.setSort(umsShopLevel.getSort());
        umsShopLevelOwn.setUploadTrendsNum(umsShopLevel.getUploadTrendsNum());
        umsShopLevelOwn.setUploadGoodsNum(umsShopLevel.getUploadGoodsNum());
        umsShopLevelOwn.setBindExpertNum(umsShopLevel.getBindExpertNum());
        umsShopLevelOwn.setStorageQuantity(umsShopLevel.getStorageQuantity());
        umsShopLevelOwn.setRecommendNum(umsShopLevel.getRecommendNum());
        umsShopLevelOwn.setVirtualCurrency(umsShopLevel.getVirtualCurrency());
        umsShopLevelOwn.setLevelDescribe(umsShopLevel.getLevelDescribe());
        umsShopLevelOwn.setIsShow(umsShopLevel.getIsShow());
        umsShopLevelOwn.setIsInitLevel(umsShopLevel.getIsInitLevel());
        umsShopLevelOwn.setAddTime(new Date());
        umsShopLevelOwn.setUiLayoutNum(umsShopLevel.getUiLayoutNum());
        umsShopLevelOwn.setGoodsMainImgDesignNum(umsShopLevel.getGoodsMainImgDesignNum());
        umsShopLevelOwnMapper.insertSelective(umsShopLevelOwn);

        UmsShopLevelPriceOwn umsShopLevelPriceOwn = new UmsShopLevelPriceOwn();
        umsShopLevelPriceOwn.setLevelOwnId(umsShopLevelOwn.getId());
        umsShopLevelPriceOwn.setTimeType(umsShopLevelPrice.getTimeType());
        umsShopLevelPriceOwn.setPrice(umsShopLevelPrice.getPrice());
        umsShopLevelPriceOwn.setIsShow(umsShopLevelPrice.getIsShow());
        umsShopLevelPriceOwn.setPriceOriginal(umsShopLevelPrice.getPriceOriginal());
        umsShopLevelPriceOwn.setShopId(shopId);
        umsShopLevelPriceOwn.setAddTime(new Date());
        umsShopLevelPriceOwnMapper.insertSelective(umsShopLevelPriceOwn);

        if (type == 1 || type == 2 || type == 3) {
            //如果类型是会员升级/增值服务-内存/增值服务-数量  才添加到shopLevelOwn表
            Calendar calendar = Calendar.getInstance();
            Date deadline = null;
            if (umsShopLevelPrice.getTimeType() == 1 || umsShopLevelPrice.getTimeType() == 4) {
                //1个月或连续1个月
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, 1);
                deadline = calendar.getTime();
            } else if (umsShopLevelPrice.getTimeType() == 2 || umsShopLevelPrice.getTimeType() == 5) {
                //1个季度或连续1个季度
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, 3);
                deadline = calendar.getTime();
            } else if (umsShopLevelPrice.getTimeType() == 3 || umsShopLevelPrice.getTimeType() == 6) {
                //1个年或连续1年
                calendar.setTime(new Date());
                calendar.add(Calendar.YEAR, 1);
                deadline = calendar.getTime();
            } else if (umsShopLevelPrice.getTimeType() == 7) {
                //1个年或连续1年
                calendar.setTime(new Date());
                calendar.add(Calendar.DAY_OF_MONTH, 15);
                deadline = calendar.getTime();
            }
            UmsShopOwnLevel umsShopOwnLevel = new UmsShopOwnLevel();


            umsShopOwnLevel.setShopId(shopId);
            umsShopOwnLevel.setLevelPriceOwnId(umsShopLevelPriceOwn.getId());
            umsShopOwnLevel.setStartTime(new Date());
            umsShopOwnLevel.setDeadline(deadline);
            umsShopOwnLevel.setResidueUploadTrendsNum(umsShopLevel.getUploadTrendsNum());
            umsShopOwnLevel.setResidueUploadGoodsNum(umsShopLevel.getUploadGoodsNum());
            umsShopOwnLevel.setResidueBindExpertNum(umsShopLevel.getBindExpertNum());
            umsShopOwnLevel.setResidueStorageQuantity(umsShopLevel.getStorageQuantity());
            umsShopOwnLevel.setResidueRecommendNum(umsShopLevel.getRecommendNum());
            umsShopOwnLevel.setAddTime(new Date());
            umsShopOwnLevel.setType(type);
            umsShopOwnLevel.setUiLayoutNum(umsShopLevel.getUiLayoutNum());
            umsShopOwnLevel.setGoodsMainImgDesignNum(umsShopLevel.getGoodsMainImgDesignNum());
            umsShopOwnLevelMapper.insert(umsShopOwnLevel);
        } else if (type == 4) {
            //如果是虚拟币，增加用户虚拟币数量
            UmsShop shopInfo = shopService.getShopById(shopId);
            UmsShop shopUpdate = new UmsShop();
            shopUpdate.setId(shopId);
            shopUpdate.setVirtualCurrency(shopInfo.getVirtualCurrency() + umsShopLevel.getVirtualCurrency());

            umsShopLevelActionLog.setNum(Double.parseDouble(umsShopLevel.getVirtualCurrency().toString()));
            umsShopLevelActionLog.setBeforeNum(shopInfo.getVirtualCurrency());
            umsShopLevelActionLog.setAfterNum(shopInfo.getVirtualCurrency() + umsShopLevel.getVirtualCurrency());
            shopService.updateShop(shopUpdate);
        }

        if (type == 1) {
            umsShopLevelActionLog.setNum(1.0);
        } else if (type == 2) {
            umsShopLevelActionLog.setNum(Double.parseDouble(umsShopLevel.getStorageQuantity().toString()));
        } else if (type == 3) {
            umsShopLevelActionLog.setNum(Double.parseDouble(umsShopLevel.getUploadTrendsNum().toString()));
        }
        umsShopLevelActionLogMapper.insert(umsShopLevelActionLog);
        return CommonResult.success(null);
    }
}